<template>
  <VPage>
    <VTable ref="tableRef" v-bind="tableOptions">
      <template #form="{ form }">
        <VSelect v-model="form.supplier_id" placeholder="供应商" type="supplier" style="min-width: 160px" multiple collapse-tags clearable filterable />
        <VSelect v-model="form.company_id" placeholder="公司主体" :options="options.company_id" multiple collapse-tags style="min-width: 210px" />
        <VSelect v-model="form.period_id" placeholder="结算方式" type="period" multiple collapse-tags style="min-width: 220px" />
        <VCascader v-model="form.department_name" placeholder="部门" type="department" multiple collapse-tags checkStrictly style="min-width: 160px" />
        <VSelect v-model="form.currency" placeholder="原币" :options="options.currency" multiple collapse-tags style="min-width: 160px" />
        <VGroup>
          <div class="v-group-item">统计月份</div>
          <el-date-picker v-model="form.dateRange.value" type="monthrange" value-format="YYYY-MM" range-separator="至" start-placeholder="开始月份" end-placeholder="结束月份"
            style="max-width: 300px" />
        </VGroup>
      </template>
      <template #toolbar_btns>
        <VTotal ref="totalRef" url="/v1/finance/payable-balances/total-department" class="w-11/12" :options="[
          { title: '累计当月付款金额', key: 'payment_price' },
          { title: '累计当月预估金额', key: 'estimate_price' },
          { title: '期末余额', key: 'end_balances' },
          { title: '期末余额', key: 'end_balances_cny' },
          ]" :child="val => (val || []).map(d => `${d.currency}: ${d.price}`).join(' ')" />
        <VExport auth="/purchase/finance/payable/export" mainKey="pb_id" name="payable-balances-department"  class="ml-auto" :tableRef="tableRef" task />
      </template>
    </VTable>
    <Adjust ref="adjustRef" :options="options" @success="query" />
    <VLog ref="logRef" />
  </VPage>
</template>
  
<script setup lang="jsx">
import api from '../api'
import VTotal from '@/components/VTotal'
import { getOption } from '@/api/public'

const baseName = 'payable'
const tableRef = ref()
const logRef = ref()
const totalRef = ref()
// 查询
const query = () => {
  tableRef?.value.query()
}

// 获取options
const options = ref({})
Promise.all([
  getOption({ type: baseName }).then(res => res.data),
  Promise.resolve({ test: [{ label: '没有数据', value: 1 }] })
]).then(res => {
  options.value = res.reduce((acc, cur) => (((acc = { ...acc, ...cur }), acc)), {})
})

// 列表配置
const tableOptions = reactive({
  rowConfig: { height: 100 },
  showHeaderOverflow: false,
  showOverflow: false,
  formConfig: {
    data: {
      dateRange: { type: 'month', value: [], range: true },
    }
  },
  columns: [
    { title: '月份', field: 'month', minWidth: 100 },
    {
      title: '公司主体', field: 'company_id_name', minWidth: 140, slots: {
        header: () => <div>公司主体</div>,
        default: ({ row }) => <VText value={row.company_id_name} line={3} />
      }
    },
    {
      minWidth: 140, slots: {
        header: () => <div>供应商信息</div>,
        default: ({ row }) => <div>
        <VText value={row.supplier_name} line={3} />
        <VText title='供应商ID' value={row.supplier_code} />
        </div>
      }
    },
    { title: '类型', field: 'payable_type_name', minWidth: 100 },
    {
      title: '结算方式', field: 'period_name', minWidth: 140, slots: {
        default: ({ row }) => <VText value={row.period_name} line={3} />
      }
    },
    {
      title: '事业处', field: 'parent_department_name', minWidth: 100, slots: {
        default: ({ row }) => <VText value={row.parent_department_name} line={3} />
      }
    },
    { title: '原币', field: 'currency_name', minWidth: 100 },
    { title: '期初余额', field: 'start_balances', minWidth: 100 },
    { title: '当月付款', field: 'month_payment_price', minWidth: 100 },
    { title: '当月暂估', field: 'month_estimate_price', minWidth: 100 },
    { title: '期末余额（原币）', field: 'end_balances', minWidth: 100 },
    { title: '期末余额（CNY）', field: 'end_balances_cny', minWidth: 100 },
  ],
  proxyConfig: {
    autoLoad: false,
    ajax: {
      query: ({ page, form }) => {
        totalRef?.value.update({ ...form, ...page })
        return api.queryDepartment({ ...form, ...page })
      }
    }
  }
})
</script>
  